namespace Plugins.Serializer
{
    using System;
    using System.Deployment.Application;
    using System.IO;
    using System.Security;
    using Plugins.Interfaces;
    using Plugins.Library;

    [Serializable]
    public class PluginFileSerializer<T> : PluginFileStreamSerializer<T>
    {
        protected override string GetFileName(string fileName)
        {
            lock (((PluginFileSerializer<T>)this))
            {
                try
                {
                    return PluginProgram.Provider.GetPath(fileName);
                }
                catch (InvalidDeploymentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                PluginLog.WriteErrorSync(ex);
                }
                return null;
            }
        }

        protected override bool Serialize(string fileName, T obj, IPluginTypeFormatter formatter)
        {
            lock (((PluginFileSerializer<T>) this))
            {
                try
                {
                    bool result = false;
                    using (FileStream stream = PluginProgram.Provider.GetFileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        result = Serialize(obj, stream, formatter);
                        stream.Close();
                    }
                    return result;
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (NotSupportedException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (PathTooLongException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (DirectoryNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IOException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return false;
            }
        }

        protected override bool Deserialize(string fileName, IPluginTypeFormatter formatter)
        {
            lock (((PluginFileSerializer<T>)this))
            {
                try
                {
                    bool result = false;
                    using (FileStream stream = PluginProgram.Provider.GetFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        result = Deserialize(stream, formatter);
                        stream.Close();
                    }
                    return result;
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (NotSupportedException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (PathTooLongException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (DirectoryNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IOException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return false;
            }
        }
    }
}

